<template>
  <div>
    <yt-crud
      v-bind="options"
      ref="crudRef"
      @onLoad="getData"
      :fun-props="{
        exportBtn: true,
        delBtn: false,
        addBtn: false,
      }"
      :table-props="{
        selection: true,
        delBtn: false,
        viewBtn: true,
        editBtn: false,
        menuSlot: true,
        menuWidth: 100,
        index: false,
      }"
      :loading="state.loading"
      :total="state.total"
      v-model:query="state.query"
      @change="onChange"
    >
      <template #rightToolbar>
        <el-button link @click="handleSettle">.</el-button>
      </template>
      <template #menuSlot></template>

      <template #startEndTime="scope:any">
        <div>{{scope.row.startTime || '-'}}</div>
        <div>{{scope.row.endTime || '-'}}</div>
      </template>
      <template #type="scope:any">
        <DictTag :options="LedgerTypeEnum" :value="scope.row.type" />
      </template>
      <template #state="scope:any">
        <DictTag type="info" :options="LedgerStateEnum" :value="scope.row.state" />
      </template>
      <template #name="scope:any">
        <div>
          <span v-if="scope.row.type == 'Platform'">-</span>
          <span v-if="scope.row.type == 'Tenant'">{{scope.row.tenantName}}</span>
          <span v-if="scope.row.type == 'Agent'">{{scope.row.agentName}}</span>
          <span v-if="scope.row.type == 'Dealer'">{{scope.row.dealerName}}</span>
        </div>
      </template>
      <template #station="scope:any">
        <div>{{translateI18nField(scope.row.stationName)}}</div>
        <div>{{scope.row.chargerDn}}</div>
        <div>{{scope.row.gunNo}}</div>
      </template>
    </yt-crud>
  </div>
</template>

<script lang="ts" setup>
import { IColumn } from '@/components/common/types/tableCommon'
import {ledgerList, ledgerSettle } from '@/api/business/ledger'
import YtCrud from '@/components/common/yt-crud.vue'
import {ComponentInternalInstance} from 'vue'
import {useI18n} from 'vue-i18n'
import {translateI18nField} from '@/utils/i18n'

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { LedgerStateEnum, LedgerTypeEnum } = toRefs<any>(proxy?.useEnum('ledger_state_enum', 'ledger_type_enum'))
const { t } = useI18n()

const state = reactive({
  total: 0,
  loading: false,
  query: {},
})


// 字典
const column = ref<IColumn[]>([
  {
    label: t('business.ledger.type'),
    key: 'type',
    align: 'center',
    type: 'select',
    search: true,
    slot: true,
    componentProps: {
      options: LedgerTypeEnum,
    },
  },
  {
    label: t('business.ledger.name'),
    key: 'name',
    type: 'string',
    search: true,
    formHide: false,
    searchLabelWidth: 120,
    slot: true,
    align: 'center',
    colSpan: 22,
  },
  {
    label: t('business.ledger.orderTranId'),
    key: 'orderTranId',
    type: 'string',
    search: true,
    formHide: false,
    colSpan: 22,
  },
  {
    label: t('business.ledger.userName'),
    key: 'userName',
    type: 'string',
    hide: true,
    search: false,
    formHide: false,
    colSpan: 22,
  },
  {
    label: t('business.ledger.station'),
    key: 'station',
    search: false,
    formHide: false,
    colSpan: 22,
    slot: true,
  },
  {
    label: t('business.ledger.startEndTime'),
    key: 'startEndTime',
    align: 'center',
    hide: false,
    search: false,
    type: 'date',
    formHide: false,
    slot: true,
  },
  {
    label: t('business.ledger.totalQuantity'),
    key: 'totalQuantity',
    align: 'center',
    search: false,
    sortable: true,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.ledger.totalAmount'),
    key: 'totalAmount',
    align: 'center',
    search: false,
    sortable: true,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.ledger.chargeMinute'),
    key: 'chargeMinute',
    align: 'center',
    type: 'number',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.ledger.settledServiceAmount'),
    key: 'settledServiceAmount',
    align: 'center',
    type: 'number',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.ledger.amount'),
    key: 'amount',
    align: 'center',
    type: 'number',
    search: false,
    formHide: false,
    slot: false,
  },
  {
    label: t('business.ledger.state'),
    key: 'state',
    align: 'center',
    type: 'select',
    componentProps: {
      options: LedgerStateEnum,
      style: {
        width: '120px'
      }
    },
    sortable: true,
    search: true,
    formHide: false,
    slot: true,
  },
  {
    label: t('crud.table.column.createTime'),
    key: 'createTime',
    tableWidth: 180,
    sortable: true,
    type: 'date',
    formHide: true,
  },
])
const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  ledgerList({
    ...state.query,
  })
    .then((res: any) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}


const handleSettle = () =>{
  ledgerSettle()
}

const multipleSelection = ref<{ deviceId: string }[]>([])

const onChange = (data) => {
  multipleSelection.value = data
}

const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>

<style lang="scss" scoped></style>
